package cn.example.web.module.user;

import cn.example.common.constant.ExampleErrorCode;
import cn.example.web.annotation.IController;
import cn.example.web.base.AbstractController;
import cn.example.web.db.bean.Channel;
import cn.example.web.db.bean.ChannelApp;
import cn.example.web.module.channel.handler.AbstractChannelHandler;
import cn.example.web.module.channel.handler.ChannelHandlerFactory;
import cn.example.web.module.channel.serivce.ChannelAppService;
import cn.example.web.module.channel.serivce.ChannelService;
import org.artifact.core.exception.ArtifactExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;

@IController("user")
public class UserController extends AbstractController {

	@Autowired
	private ChannelAppService channelAppService;
	
	@Autowired
	private ChannelService channelService;

	public void login() {
		int channelPackId = getParaToInt("channelPackId", 0);
		ChannelApp channelApp = channelAppService.findByPrimaryKey(channelPackId);
		ArtifactExceptionUtil.isTrue(ExampleErrorCode.ILLEGAL_APP, "非法App包", channelApp == null);

		// 取得所属渠道
		Channel channel = channelService.findByPrimaryKey(channelApp.getChannelId());
		
		// 渠道对应渠道处理器
		AbstractChannelHandler handler = ChannelHandlerFactory.getHandler(channel);
		
		// 检查参数
		ArtifactExceptionUtil.isTrue(ExampleErrorCode.MISSING_PARAMETER, "缺少参数",!handler.checkLoginParameter(this));

		// 验证登陆
		UserVO userVO = handler.login(this,channel);

		renderSuccess(userVO.getSign());
	}
}
